#include <glib.h>

static gint s_count = 5;

static gboolean idle_add_func(gpointer data)
{
    GMainLoop *loop = (GMainLoop *)data;
    g_return_if_fail(loop);

    g_print("%s: sleep 500ms and continue \n", __FUNCTION__);
    if(s_count--) {
        g_usleep(500*1000);
        return TRUE;
    }

    g_print("%s: quit mainloop \n", __FUNCTION__);
    g_main_loop_quit(loop);

    return FALSE;
}

gint main (gint argc, gchar **argv)
{
    GMainLoop *loop = NULL;

    loop = g_main_loop_new(NULL, TRUE);

    g_idle_add(idle_add_func, loop);

    g_main_loop_run(loop);

    g_main_loop_unref(loop);

    return 0;
}
